<template>
 <!--  委外-->
 <div class="content">
  <div class="pageSearchView">
   <el-form :inline="true" :model="searchParam" class="demo-form-inline">
    <el-form-item label="委外计划名称">
     <el-input v-model="searchParam.name" placeholder="请输入质检方案编码" clearable style="width: 200px" />
    </el-form-item>
    <el-form-item label="委外计划编码">
     <el-input v-model="searchParam.code" placeholder="请输入质检方案名称" clearable style="width: 200px" />
    </el-form-item>
    <el-form-item>
     <el-button type="primary" :icon="Search" @click="getList()">查询</el-button>
     <el-button class="refreshBtn" :icon="Refresh" @click="resetSearch()">重置</el-button>
    </el-form-item>
   </el-form>
  </div>
  <div class="pageTableView">
   <div class="pageTableMenuView">
    <div class="pageTableTitle">列表信息</div>
   </div>
   <!-- 表格 -->
   <el-table class="pageTableClass" :data="paginatedData" border height="calc(100vh - 320px)">
    <el-table-column label="序号" align="center" width="70">
     <template #default="scope">
      <!-- 下标 + 1，计算公式：当前页数 * 每页大小 + 当前行的索引 + 1 --> {{ (pageSearch.current - 1) * pageSearch.pageSize + scope.$index + 1
      }} </template>
    </el-table-column>
    <el-table-column prop="outsourcingNo" label="委外单号" align="center" min-width="200" />
    <el-table-column prop="procedureOrderNo" label="工序订单号" align="center" min-width="200" />
    <el-table-column label="委外数量" align="center" prop="outsourcingCount" min-width="200" />
    <el-table-column prop="demandTime" label="需求日期" min-width="150" align="center">
     <template #default="scope"> {{ scope.row.demandTime.slice(0, 10) }} </template>
    </el-table-column>
    <el-table-column prop="supplierName" label="供应商" align="center" min-width="250" />
    <el-table-column prop="putWarehouseName" width="250" label="产品入库仓库" align="center" />
    <el-table-column prop="supplierWarehoseName" width="250" label="供应商材料库" align="center" />
    <el-table-column prop="warehouseName" label="发货仓库" width="250" align="center" />
    <el-table-column prop="planCount" width="250" label="计划数量" align="center" />
    <el-table-column prop="status" label="单据状态" align="center" min-width="200">
     <template #default="scope">
      <span> {{ getQualityTypeByValue(scope.row.receiptsStatus) }} </span>
     </template>
    </el-table-column>
   </el-table>
   <div class="pagePagination">
    <el-pagination background v-model:current-page="pageSearch.current" v-model:page-size="pageSearch.pageSize"
     :page-sizes="[10, 20, 50, 100, 200, 500]" layout="total, sizes, prev, pager, next" :total="pageTotal"
     @size-change="handleSizeChange" @current-change="handleCurrentChange" />
   </div>
  </div>
 </div>
</template>
<script lang="ts" setup>
import { computed, reactive, ref } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
import axiosInstance from '@/utils/http/api';
import { Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue';

onMounted(() => {
 getList();
 getOption();
});


const pageSearch = ref({
 current: 1,
 pageSize: 10
});


const workOrderList = ref({});


const pageTotal = ref(null);//页面总数
const searchParam = ref({});//页面检索条件



// 判断质检类型
const getQualityTypeByValue = (e) => {
 let name = null;
 for (const item of qualityTypeList) {
  if (e == item.value) {
   name = item.label;
  }
 }
 return name;
};

// 质检类型
const qualityTypeList = reactive([
 {
  label: '部分送货',
  value: '1',
 },
 {
  label: '全部送货',
  value: '2',
 },
 {
  label: '未送货',
  value: '0',
 }
 // 继续添加更多检查类型...
]);


const paginatedData = ref([]);
const materiaList = ref([]);//物料表
const wareList = ref([]);//仓库
const procedureOrderList = ref([]);//工序订单
const supplierList = ref([]);
const storageAllList = ref([]);
// 供应商远端搜索
const supplierSearch = async (query: string) => {
 if (query) {
  try {
   const response = await axiosInstance.post(`/api/basedataSupplier/getPage`, {
    current: 1,
    pageSize: 20,
    name: query
   });
   supplierList.value = await response.data.records; // 假设返回的数据直接是供应商列表
  } catch (error) {
   supplierList.value = []; // 出错时清空列表
  }
 }
};

const getOption = () => {
 axiosInstance.post('/api/outsourcingPlan/querySourcingPlanProcedureOrder', {})
  .then(res => {
   workOrderList.value = res.data;
  });

 // 查询所有库位
 axiosInstance.post('/api/basedataStorage/getAll', {})
  .then(res => {
   storageAllList.value = res.data;
  });


 axiosInstance.post(`/api/basedataSupplier/getPage`, {
  current: 1,
  pageSize: 20
 }).then(res => {
  if (res.code == 200) {
   supplierList.value = res.data.records;
  }
 });


 axiosInstance.post('/api/planProcedureOrder/getAll', {})
  .then(res => {
   if (res.code !== 200) {
    ElMessage.error(res.message);
    return false;
   }
   const list = res.data;
   const loop = [];
   for (const item of list) {
    loop.push(
     {
      value: item.id,
      label: item.materialName
     }
    );
   }
   procedureOrderList.value = list;
  });

 axiosInstance.get('/api/basedataWarehouse/treeList', {}).then(res => {
  if (res.code !== 200) {
   ElMessage.error(res.message);
   return false;
  }
  wareList.value = res.data;
 });
};

// 获取列表接口
const getList = () => {
 axiosInstance.post('/api/outsourcingPlan/getPage', {
  ...searchParam.value,
  ...pageSearch.value
 })
  .then(res => {
   if (res.code !== 200) {
    ElMessage.error(res.message);
    return false;
   }
   const list = res.data.records;
   for (const item of list) {
    item.children = item.outsourcingWorkMaterialVOS;
   }
   paginatedData.value = list;
   pageTotal.value = res.data.total;
  });
};

const handleSizeChange = (e) => {
 pageSearch.value.pageSize = e;
 getList();
};

const handleCurrentChange = (e) => {
 pageSearch.value.current = e;
 getList();
};

// 重置搜索条件
const resetSearch = () => {
 searchParam.value = {};
 pageSearch.value.current = 1;
 // current.value = 1
 getList();
};

</script>
<style scoped></style>
